// ControlView.cpp : implementation file
//

#include "stdafx.h"
#include "MFC_OpenGl.h"
#include "ControlView.h"

// ControlView

IMPLEMENT_DYNCREATE(ControlView, CFormView)

ControlView::ControlView()
	: CFormView(ControlView::IDD)
{
}

ControlView::~ControlView()
{
}

void ControlView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(ControlView, CFormView)
	ON_BN_CLICKED(IDC_RADIO1, &ControlView::OnBnClickedRadio1)
	ON_BN_CLICKED(IDC_BUTTON1, &ControlView::OnBnClickedButton1)
	ON_WM_CREATE()
	ON_BN_CLICKED(IDC_BUTTON2, &ControlView::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_RADIO3, &ControlView::OnBnClickedRadio3)
	ON_BN_CLICKED(IDC_RADIO4, &ControlView::OnBnClickedRadio4)
	ON_BN_CLICKED(IDC_RADIO5, &ControlView::OnBnClickedRadio5)
	ON_BN_CLICKED(IDC_RADIO6, &ControlView::OnBnClickedRadio6)
	ON_BN_CLICKED(IDC_RADIO7, &ControlView::OnBnClickedRadio7)
	ON_BN_CLICKED(IDC_RADIO8, &ControlView::OnBnClickedRadio8)
	ON_BN_CLICKED(IDC_RADIO9, &ControlView::OnBnClickedRadio9)
	ON_BN_CLICKED(IDC_RADIO10, &ControlView::OnBnClickedRadio10)
END_MESSAGE_MAP()


// ControlView diagnostics

#ifdef _DEBUG
void ControlView::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void ControlView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// ControlView message handlers


void ControlView::OnBnClickedRadio1()
{
	// TODO: Add your control notification handler code here
}


void ControlView::OnBnClickedButton1()
{
	CString str1;
	CString str2;
	CString str3;
	CEdit* pED1=(CEdit*)GetDlgItem(IDC_EDIT1);
	CEdit* pED2=(CEdit*)GetDlgItem(IDC_EDIT2);
	CEdit* pED3=(CEdit*)GetDlgItem(IDC_EDIT3);
	
	pED1->GetWindowText(str1);
	pED2->GetWindowText(str2);
	pED3->GetWindowText(str3);
	/*str1.Insert(0,L"0");
	str2.Insert(0,L"0");
	str3.Insert(0,L"0");*/
	if(!this->CheckIsNumeric((std::wstring)str1))
	{
		AfxMessageBox(L"The X-Position should be a number");
		return;
	}
	float x=str1.IsEmpty()?0.0f: std::stof((std::wstring)str1);
	if(!this->CheckIsNumeric((std::wstring)str2))
	{
		AfxMessageBox(L"The Y-Position should not be a number");
		return;
	}
	float y=str2.IsEmpty()?0.0f: std::stof((std::wstring)str2);
	if(!this->CheckIsNumeric((std::wstring)str3))
	{
		AfxMessageBox(L"The Z-Position should not be a number");
		return;
	}
	float z=str3.IsEmpty()?0.0f: std::stof((std::wstring)str3);
	this->leftView->SetGamePosition(x,y,z);
}

bool ControlView::CheckIsNumeric(const std::wstring &str)
{
	return str.find_first_not_of(L"-0123456789.") == std::wstring::npos 
		&& count(str.begin(), str.end(), '.') <= 1;
}


int ControlView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	CSplitterWnd *split= (CSplitterWnd*)GetParent();
	this->leftView=(CMFC_OpenGlView*)split->GetPane(0,0);

	this->x=1.0f;
	this->y=0.0f;
	this->z=0.0f;
	this->index=0;
	this->direction=1;
	return 0;
}


void ControlView::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	this->leftView->AddOperation(new MagicCubeOperation(this->x,this->y,this->z,this->index,0,this->direction));
	
}


void ControlView::OnBnClickedRadio3()
{
	// TODO: Add your control notification handler code here
	this->x=1.0f;
	this->y=0.0f;
	this->z=0.0f;
}


void ControlView::OnBnClickedRadio4()
{
	// TODO: Add your control notification handler code here
	this->x=0.0f;
	this->y=1.0f;
	this->z=0.0f;
}


void ControlView::OnBnClickedRadio5()
{
	// TODO: Add your control notification handler code here
	this->x=0.0f;
	this->y=0.0f;
	this->z=1.0f;
}


void ControlView::OnBnClickedRadio6()
{
	// TODO: Add your control notification handler code here
	this->direction=1;
}


void ControlView::OnBnClickedRadio7()
{
	this->direction=-1;
	// TODO: Add your control notification handler code here
}


void ControlView::OnBnClickedRadio8()
{
	// TODO: Add your control notification handler code here
	this->index=0;
}


void ControlView::OnBnClickedRadio9()
{
	// TODO: Add your control notification handler code here
	this->index=1;
}


void ControlView::OnBnClickedRadio10()
{
	// TODO: Add your control notification handler code here
	this->index=2;
}


void ControlView::OnInitialUpdate()
{
	CEdit* pED1=(CEdit*)GetDlgItem(IDC_EDIT1);
	CEdit* pED2=(CEdit*)GetDlgItem(IDC_EDIT2);
	CEdit* pED3=(CEdit*)GetDlgItem(IDC_EDIT3);
	pED1->SetWindowTextW(L"0");
	pED2->SetWindowTextW(L"0");
	pED3->SetWindowTextW(L"0");
	((CButton*)GetDlgItem(IDC_RADIO3))->SetCheck(true);
	((CButton*)GetDlgItem(IDC_RADIO6))->SetCheck(true);
	((CButton*)GetDlgItem(IDC_RADIO8))->SetCheck(true);
	CFormView::OnInitialUpdate();

	// TODO: Add your specialized code here and/or call the base class
}
